import asyncio
import pygame
import edge_tts

VOICE = "zh-CN-XiaoxiaoNeural"
OUTPUT_FILE = "temp.mp3"


async def speak(TEXT):
    try:
        communicate = edge_tts.Communicate(TEXT, VOICE, rate='+10%')
        await communicate.save(OUTPUT_FILE)
    except Exception as e:
        print(f"An error occurred during TTS: {e}")


if __name__ == "__main__":
    while True:
        TEXT = input()
        pygame.init()
        asyncio.run(speak(TEXT))
        pygame.mixer.music.load(OUTPUT_FILE)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            pygame.time.Clock().tick(10)
        pygame.quit()